﻿using System;
using System.Linq;
using System.Threading.Tasks;
using System.Collections.Generic;
using WaterCloud.Code;
using SqlSugar;
using WaterCloud.DataBase;
using WaterCloud.Domain.SalaryManagement;
using WaterCloud.Domain.AttendanceManagement;

namespace WaterCloud.Service.SalaryManagement
{
    /// <summary>
    /// 创 建：超级管理员
    /// 日 期：2025-04-23 11:23
    /// 描 述：薪酬方案服务类
    /// </summary>
    public class PersonsalaryService : BaseService<PersonsalaryEntity>, IDenpendency
    {
        public PersonsalaryService(ISqlSugarClient context) : base(context)
        {
        }
        #region 获取数据
        public async Task<List<PersonsalaryEntity>> GetList(string keyword = "")
        {
            var data = repository.IQueryable();
            if (!string.IsNullOrEmpty(keyword))
            {
                data = data.Where(a => a.F_EnCode.Contains(keyword)
                || a.F_FullName.Contains(keyword));
            }
            return await data.Where(a => a.F_DeleteMark == false).OrderBy(a => a.F_Id, OrderByType.Desc).ToListAsync();
        }

        public async Task<List<PersonsalaryEntity>> GetLookList(string keyword = "")
        {
            var query = repository.IQueryable().Where(a => a.F_DeleteMark == false);
            if (!string.IsNullOrEmpty(keyword))
            {
                //此处需修改
                query = query.Where(a => a.F_EnCode.Contains(keyword)
                || a.F_FullName.Contains(keyword));
            }
            //权限过滤
            query = GetDataPrivilege("a", "", query);
            return await query.OrderBy(a => a.F_Id, OrderByType.Desc).ToListAsync();
        }

        public async Task<List<PersonsalaryEntity>> GetLookList(SoulPage<PersonsalaryEntity> pagination, string keyword = "", string id = "")
        {
            var query = repository.Db.Queryable<PersonsalaryEntity, BasicsalaryEntity, JobsalaryEntity, PerformancesalaryEntity>((a, b, c, d) => new JoinQueryInfos(
             JoinType.Left, a.F_BasicSalaryNum == b.F_EnCode,
               JoinType.Left, a.F_JopSalaryNum == c.F_EnCode,
                 JoinType.Left, a.F_PerformanceSalaryNum == d.F_EnCode
                
             ))
         .Select((a, b,c,d) => new PersonsalaryEntity
         {
             F_Id = a.F_Id.SelectAll(),

            F_BasicSalary = b.F_FullName,
            F_JopSalary= d.F_FullName,
            F_PerformanceSalary = d.F_FullName,

         }).MergeTable();
           
            if (!string.IsNullOrEmpty(keyword))
            {
                query = query.Where(a => a.F_EnCode.Contains(keyword)
                || a.F_FullName.Contains(keyword));
            }
            if (!string.IsNullOrEmpty(id))
            {
                query = query.Where(a => a.F_Id == id);
            }
            //权限过滤
            query = GetDataPrivilege("a", "", query);
            return await query.ToPageListAsync(pagination);
        }

        public async Task<PersonsalaryEntity> GetForm(string keyValue)
        {
            var data = await repository.FindEntity(keyValue);
            return data;
        }
        #endregion

        public async Task<PersonsalaryEntity> GetLookForm(string keyValue)
        {
            var data = await repository.FindEntity(keyValue);
            return GetFieldsFilterData(data);
        }

        #region 提交数据
        public async Task SubmitForm(PersonsalaryEntity entity, string keyValue)
        {
            if (string.IsNullOrEmpty(keyValue))
            {
                //初始值添加
                entity.F_DeleteMark = false;
                entity.Create();
                await repository.Insert(entity);
            }
            else
            {
                //此处需修改
                entity.Modify(keyValue);
                await repository.Update(entity);
            }
        }

        public async Task DeleteForm(string keyValue)
        {
            var ids = keyValue.Split(',');
            await repository.Delete(a => ids.Contains(a.F_Id.ToString()));
        }
        #endregion

    }
}
